package conch.yaoms.jni;

import java.util.ArrayList;


public class MemoryWatcher {
	
	
	
	
	public native ArrayList<ProcessItem> getProcessList();
	
	public native void openProcess(int processId);
	
	public native byte[] getBytes(int processId, int address, int count);
	
	public native void closeProcess();
	
	public native boolean isReady();
	
	static {
		System.loadLibrary("memwtr");
	}
	
	
	
	
	
	
	
	
	
	
	
	private int processId;

	private int hp_address;
	
	private int max_hp_address;

	private int mp_address;
	
	private int max_mp_address;
	
	public int getMp_address() {
		return mp_address;
	}

	public void setMp_address(int mpAddress) {
		mp_address = mpAddress;
	}

	public int getMax_mp_address() {
		return max_mp_address;
	}

	public void setMax_mp_address(int maxMpAddress) {
		max_mp_address = maxMpAddress;
	}

	public int getMax_hp_address() {
		return max_hp_address;
	}

	public void setMax_hp_address(int maxHpAddress) {
		max_hp_address = maxHpAddress;
	}

	public int getProcessId() {
		return processId;
	}

	public void setProcessId(int processId) {
		this.processId = processId;
		closeProcess();
		openProcess(processId);
	}

	public int getHp_address() {
		return hp_address;
	}

	public void setHp_address(int hpAddress) {
		hp_address = hpAddress;
	}
	
	public int getHp() {
		if (!isReady()) return -1;
		byte[] buf = getBytes(processId, hp_address, 4);
		return bytes4Toint(buf);
	}
	
	public int getMaxHp() {
		if (!isReady()) return -1;
		byte[] buf = getBytes(processId, max_hp_address, 4);
		return bytes4Toint(buf);
	}
	
	public int getMp() {
		if (!isReady()) return -1;
		byte[] buf = getBytes(processId, mp_address, 4);
		return bytes4Toint(buf);
	}
	
	public int getMaxMp() {
		if (!isReady()) return -1;
		byte[] buf = getBytes(processId, max_mp_address, 4);
		return bytes4Toint(buf);
	}
	
	public int bytes4Toint(byte[] bytes) {
		int in2 = ((bytes[0] << 24) & 0xFF000000)
				| ((bytes[1] << 16) & 0x00FF0000)
				| ((bytes[2] << 8) & 0x0000FF00) | (bytes[3] & 0x000000FF);
		return in2;
	}
}
